package com.mongo;

import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.vo.ShortUrlVo;

@Service
public class ShortUrlVoMongoQuery extends AbstractMongoQuery<ShortUrlVo>{

	@Override
	protected Class<ShortUrlVo> entityClass() {
		return ShortUrlVo.class;
	}

	public void upSert(ShortUrlVo shortUrlVo,String collectionName){
		Query query=new Query(Criteria.where("url").is(shortUrlVo.getUrl()));
		Update update=new Update();
		update.set("url", shortUrlVo.getUrl());
		update.set("shortUrl", shortUrlVo.getShortUrl());
		if(shortUrlVo.getKey()!=null&&!shortUrlVo.equals("")){
			update.set("key",shortUrlVo.getKey());
		}
		update.inc("count", 1);
		mongoTemplate.findAndModify(query, update, new FindAndModifyOptions().upsert(true),this.entityClass(),collectionName);
	}
}
